//
//  ZYCProfileModifyUserInfoView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/20.
//

#import "ZYCProfileModifyUserInfoView.h"
#import "UnderLinerTextField.h"

#define MAX_STARWORDS_LENGTH 20
@interface ZYCProfileModifyUserInfoView()<UITextFieldDelegate>

@property (nonatomic, strong) UnderLinerTextField *textField;
@property (nonatomic, strong) UILabel *tipLabel;
@end
@implementation ZYCProfileModifyUserInfoView

- (void)setType:(ZYCModifyUserInfoView)type{
    if (type == ZYCModifyUserInfoViewUserName) {
        
    }else{
        self.textField.keyboardType = UIKeyboardTypePhonePad;
    }
}
- (void)setModifyContent:(NSString *)modifyContent{
    _modifyContent = modifyContent;
    _textField.text = modifyContent;
}

- (void)setPlaceHolder:(NSString *)placeHolder{
    _placeHolder = placeHolder;
    _textField.placeholder = placeHolder;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.textField];
        [self addSubview:self.tipLabel];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(74.0f);
            make.left.equalTo(self).offset(23.0f);
            make.right.equalTo(self).offset(-23.0f);
        }];
        
        [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textField.mas_bottom).offset(13.0f);
            make.centerX.equalTo(self);
        }];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldEditChanged:)
                  name:UITextFieldTextDidChangeNotification object:self.textField];
    }
    return self;
}

- (UnderLinerTextField *)textField {
    if (!_textField) {
        _textField = [[UnderLinerTextField alloc] init];
        _textField.font = kSystemBoldFont(18.0f);
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.textColor = HEX_RGB(@"#313733");
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _textField;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        _tipLabel.textColor = HEX_RGB(@"#999999");
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        NSString *titleStr = @"支持20位字符以内的中英文、数字";
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:titleStr];
        [attrStr addAttribute:NSForegroundColorAttributeName
                        value:HEX_RGB(@"#009B4C")
                        range:NSMakeRange(2, 2)];
        _tipLabel.attributedText = attrStr;

    }
    return _tipLabel;
}

- (void)setIsShowTip:(BOOL)isShowTip{
    _isShowTip = isShowTip;
    self.tipLabel.hidden = !isShowTip;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"%@",textField.text);
}


- (NSString *)text{
    
    return self.textField.text;
}

#pragma mark - Notification Method
-(void)textFieldEditChanged:(NSNotification *)noti{
    UITextField *textField = (UITextField *)noti.object;
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > MAX_STARWORDS_LENGTH)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}



@end
